package com.dd.controller;

import com.dd.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//@Controller
//@ResponseBody
//@Controller + @ResponseBody = @RestController
@RestController
public class UserController {

    @RequestMapping("sayHello")
    public String sayHello() {
        System.out.println("ni hao");

        return "ni hao 彭老登";
    }

    //    @RequestMapping(value = "getParam",method = RequestMethod.GET)
    //Get请求单个参数
    @GetMapping("getParam")
    public String getParam(String name) {
        System.out.println(name);

        return name;
    }

    //get请求多个参数
    @GetMapping("getParams")
    public String getParams(String name, Integer age, String sex) {
        System.out.println("普通参数传递 name ==> " + name);
        System.out.println("普通参数传递 age ==> " + age);
        System.out.println("普通参数传递 sex ==> " + sex);

        return name;
    }

    //Post请求 获取form表单数据
    @PostMapping("postParam")
    public String postParam(@RequestParam("userName") String name, Integer age, String address) {
        System.out.println("post请求参数 name ==> " + name);
        System.out.println("普通参数传递 age ==> " + age);
        System.out.println("普通参数传递 address ==> " + address);

        return name;
    }

    //对象参数

    @GetMapping("getPojoParams")
    public String postPojoParams(User user) {
        System.out.println("接收Pojo参数 user==> " + user);
        // 接收嵌套参数
        System.out.println("接收嵌套参数 role==> " + user.getRole());

        return "Pojo参数接收成功";
    }


    //接收数组请求参数
    @GetMapping("arrayParams")
    public String arrayParams(String[] hobbies) {
        System.out.println("接收数组参数 hobbies ==> " + Arrays.toString(hobbies));

        return "数组参数接收成功";
    }

    //接收List集合请求参数
    @GetMapping("ListParams")
    public String ListParams(@RequestParam List<String> hobbies) {
        System.out.println("接收List参数 hobbies ==> " + hobbies);

        return "List集合参数接收成功";
    }

    //接收前端json数据
    @GetMapping("getJsonParam")
    public String jsonParam(@RequestBody User user) {
        System.out.println("接收json参数数据传递  ==> " + user);
        //接收嵌套参数
        System.out.println("接收嵌套参数数据传递 ==> " + user.getRole());


        return "接收前端json数据";
    }


    // 接收json数组
    @PostMapping("postJsonParam")
    public String postJsonParam(@RequestBody List<User> users) {

        System.out.println("接收json参数数据传递  ==> " + users);

        return "接收前端json数组";
    }

    // 接收日期参数  m默认格式  YYYY/MM/dd
    @GetMapping("getDateParam")
    public String getDateParam(Date date) {
        System.out.println("接收到的日期参数 ===> " + date);

        return "接收日期参数";
    }

    //接收指定日期格式参数
    @GetMapping("getDateFormatParam")
    public String getDateFormatParam(Date date, @DateTimeFormat(pattern = "yyyy-MM-dd")Date date1) {
        System.out.println("接收到的日期参数 ===> " + date);
        System.out.println("接收到的日期参数(yyyy-MM-dd) ===> " + date1);

        return "接收日期参数";
    }
}
